<?php
/*
 * 21世纪房车网
 * @package    autoimport
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: b15cdb9cef382fee5d3a6b6eb73cdeebb83a485f $
 */
set_time_limit(0);
include '../../common.inc.php';
$hd = new ImportLoop();
$hd->setAim('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$hd->setSource('sqlite:d:/caiji/393/SpiderResult.db3', '', '');
$hd->setSourceInfo('content', 'id', '');
$params = array(
	'maxid'     => isset($_GET['maxid']) ? $_GET['maxid'] : 0,
	'id'        => isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : 0,
	'startTime' => isset($_GET['startTime']) ? $_GET['startTime'] : time(),
	'limit'     => 1,
	'interval'  => 1,
	'debug'     => '',
);
$fields = array(
	'time' => 'time', // 时间字段
	'title' => 'title', // 标题字段
	'id' => 'ID', // id
	'copyfrom' => 'comefrom', // from
	'moduleid' => '23', // modelid
	'userid' => '1', // userid
	'catid' =>  3592 , // 媒体试驾 -> 试驾体验
	'username' => 'xddlmaster', // id
	'picAdd' => 'http://www.21rv.com/', // id
);
$hd->initParams($params);
$sql    = 'select * from ' . $hd->getSourceTable() . ' where id>' . $hd->getParam('id') . ' and 已发=0 order by id ASC limit ' . $hd->getParam('limit');

$result = $hd->getDbSource()->query($sql);
$rs     = $result->fetchAll();

if (!$rs) {
	$hd->times();
	$hd->end();
} else {
	$carFolders = array();
	foreach ($rs as $k => $v) {
		$insert = array();
		$hd->setParam('id', $v[$fields['id']]);     // set id

		// set handler over
		$sqlUpdate = Sql::update($hd->getSourceTable(), '已发 =1', "id={$v[$fields['id']]}");

		// filter time
		if (!AutoImport::checkTime($v[$fields['time']])) {
			$hd->getDbSource()->query($sqlUpdate);
			continue;
		}

		$ids = AutoImport::match($v[$fields['title']]);
		$time = AutoImport::getTime($v['time'], 2);
//		$time = time()+86400*10;
		$content = $v['content-1'].$v['content-2'].$v['content-3'].$v['content-4'].$v['content-5'].$v['content-6'].$v['content-7'].$v['content-8'].$v['content-9'];
		$content = substr($content, 0, strrpos($content, '[pagebreak]'));
		$content = Post::clearLink($content);
		$thumb = AutoImport::saveThumb($content, 1, 150, 113, $fields['moduleid'], $fields['userid'], $time, $fields['picAdd']);
		$insert['title'] = Str::addSlashes($v[$fields['title']]);
		$insert['subtitle'] = '';
		$insert['arbid'] = $ids[0];
		$insert['catid'] = $fields['catid'];
		$insert['armid']     = $ids[1];
		$insert['arcid']     = $ids[2];
		$insert['introduce'] = Str::addSlashes(Str::cut(strip_tags($v[$fields['content']]), 120));
		$insert['copyfrom']  = $v[$fields['copyfrom']];
		$insert['fromurl']  = '';
		$insert['hits']  = rand(20, 5000);
		$insert['thumb']  = $thumb;
		$insert['username']  = $fields['username'];
		$insert['addtime']  = $time;
		$insert['editor']  = $fields['username'];
		$insert['edittime']  = $time;
		$insert['ip']  = '60.208.115.170';
		$insert['status']  = 3;
		$insert['islink']  = 0;

		$sql = Sql::insert('xd_guide', $insert);
		if ($hd->getDbAim()->query($sql)) {
			$insertId = $hd->getDbAim()->lastInsertId();
			$insertContent = array();
			$content = AutoImport::saveRemote($content, 'jpg|jpeg|gif|png|bmp', $fields['moduleid'], $fields['userid'], $time, $fields['picAdd']);
			$insertContent['itemid'] = $insertId;
			$insertContent['content'] = Str::addSlashes($content);
			$sql = Sql::insert('xd_guide_data', $insertContent);
			if ($hd->getDbAim()->query($sql)) {
				$hd->getDbSource()->query($sqlUpdate);
				var_dump($hd->times());
				var_dump($v[$fields['id']], $v[$fields['title']]);
			} else {
				exit($sql);
			}
		} else {
			exit($sql);
		}
	}

}
var_dump($hd->getParams());
echo $hd->start($hd->getParams());